package com.yoomiito.app.ui.my.remain.detail

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.yoomiito.app.Constant
import com.yoomiito.app.adapter.my.MyMoneyDetailAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.model.my.MyMoneyDetailInfo
import com.yoomiito.app.ui.RecyclerViewBaseActivity
import com.yoomiito.app.ui.my.remain.MyRemainActivity
import com.yoomiito.app.ui.my.youmi.YouMiActivity
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.RouterUtils
import com.yoomiito.app.widget.MyItemDecoration
import java.text.FieldPosition

/**
 * 余额明细
 */
class MyMoneyDetailActivity : RecyclerViewBaseActivity<MyMoneyDetailPresenter>() {
    private var mAdapter: MyMoneyDetailAdapter? = null
    private var isAgent: Boolean = false
    private var mDetailType: String? = null//详情类型  余额明细  优米明细  电子币明细
    private var type : Int = 1

    companion object {
        val IS_AGENT = "is_agent"
        fun start(context: Context, detailType: String,type:Int) {
            val intent = Intent(context, MyMoneyDetailActivity::class.java)
            intent.putExtra(Constant.TYPE, detailType)
            intent.putExtra("types",type)
            context.startActivity(intent)
        }
    }

    override fun newP(): MyMoneyDetailPresenter {
        return MyMoneyDetailPresenter(App.mAppConfig)
    }


    override fun initData(savedInstanceState: Bundle?) {
        val intent = intent
        isAgent = intent.getBooleanExtra(IS_AGENT, false)
        mDetailType = intent.getStringExtra(Constant.TYPE)
        type = intent.getIntExtra("types",1)
        super.initData(savedInstanceState)
        initView()
    }

    private fun initView() {
        val linearLayoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        mRecyclerView.addItemDecoration(
            MyItemDecoration(
                context,
                androidx.recyclerview.widget.RecyclerView.VERTICAL
            )
        )
        mRecyclerView.layoutManager = linearLayoutManager

        mAdapter = MyMoneyDetailAdapter(null)
        mRecyclerView.adapter = mAdapter
        mAdapter?.setOnItemClickListener(BaseQuickAdapter.OnItemClickListener { adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int ->
            if (mAdapter?.data?.get(position)?.show == 1) {
                if(mAdapter?.data?.get(position)?.isShow == false){
                    mAdapter?.data?.get(position)?.id?.let {
                        mAdapter?.data?.get(position)?.bizType?.let { it1 ->
                            p.withdrawDetail(
                                position, it,
                                it1
                            )
                        }
                    }
                }else{
                    mAdapter?.data?.get(position)?.isShow = false
                    mAdapter?.setShow(false)
                }

            }

        })
        mSmartRefreshLayout.setPadding(0, 0, 0, 0)
    }

    fun setMoneyDetail(position: Int, data: MyMoneyDetailInfo.DataBean) {
        if (data != null) {
            val curData = mAdapter?.data?.get(position)
            curData?.orderId = data.orderId
            curData?.status = data.status
            curData?.ctime = data.ctime
            curData?.statusStr = data.statusStr
            curData?.image = data.image
            curData?.nickname = data.nickname
            curData?.isShow = true
            mAdapter?.data?.get(position)?.let { curData }
            mAdapter?.setShow(true)
        }
    }

    fun setMoneyDetailData(data: MyMoneyDetailInfo?) {
        if (data != null && data.data != null) {
            if (currentPage == 1) {
                if (data.data == null || data.data.size == 0) {
                    showEmptyView()
                } else {
                    mAdapter!!.setNewData(data.data)
                }
                mSmartRefreshLayout.finishRefresh(true)
            } else {
                mSmartRefreshLayout.finishLoadMore(true)
                if (data.data == null || data.data.size == 0) {
                    mSmartRefreshLayout.finishLoadMoreWithNoMoreData()
                } else {
                    mAdapter!!.addData(data.data)
                }
            }
        } else {
            finishRefresh()
        }
    }

    override fun useMyItemDecoration(): Boolean {
        return true
    }

    override fun getNewData() {
        when (mDetailType) {
            YouMiActivity.YouMi -> p.getYouMiDetail(currentPage)
            MyRemainActivity.StarCoin -> p.starStoreCoinDetail(currentPage)
            else -> p.getMoneyDetailData(currentPage,type)
        }
    }

    override fun getEmptyContent(): String {
        return when (mDetailType) {
            YouMiActivity.YouMi -> "无优米明细"
            MyRemainActivity.StarCoin -> "无电子币明细"
            else -> "无余额明细"
        }
    }

    fun finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true)
        mSmartRefreshLayout.finishLoadMore(true)
    }

}
